package com.example.mybaiduweishi.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mybaiduweishi.MyApplication;
import com.example.mybaiduweishi.R;
import com.example.mybaiduweishi.activity.base.BaseActivity;
import com.example.mybaiduweishi.db.BaiBaoXiangDao;
import com.example.mybaiduweishi.listener.OnRecyclerItemClickListener;
import com.example.mybaiduweishi.utils.DividerGridItemDecoration;
import com.example.mybaiduweishi.utils.MyItemTouchCallback;
import com.example.mybaiduweishi.utils.MyUtil;
import com.example.mybaiduweishi.utils.VibratorUtil;
import com.example.mybaiduweishi.vo.BaiBaoXiang;
import com.socks.library.KLog;

import java.util.Collections;
import java.util.List;

import butterknife.BindView;

public class BBXActivity extends BaseActivity implements MyItemTouchCallback.OnDragListener {
    @BindView(R.id.bbx_recyclerView)
    RecyclerView bbxRecyclerView;

    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    //从数据库取出数据

    BaiBaoXiangDao bbxDAo = MyApplication.getDaoSession().getBaiBaoXiangDao();;
    final List<BaiBaoXiang> list = bbxDAo.loadAll();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getContentXML() {
        return R.layout.activity_bbx;
    }

    @Override
    public void initData() {
        KLog.e(list.size());
        mAdapter = new MyAdapter(list);
        final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new MyItemTouchCallback((MyItemTouchCallback.ItemTouchAdapter) mAdapter).setOnDragListener(this));
        itemTouchHelper.attachToRecyclerView(bbxRecyclerView);

        mLayoutManager = new GridLayoutManager(this,4);
        bbxRecyclerView.setLayoutManager(mLayoutManager);
        bbxRecyclerView.setHasFixedSize(true);
        bbxRecyclerView.addItemDecoration(new DividerGridItemDecoration(this));
        // 设置item动画
        bbxRecyclerView.setItemAnimator(new DefaultItemAnimator());
        bbxRecyclerView.addOnItemTouchListener(new OnRecyclerItemClickListener(bbxRecyclerView) {
            @Override
            public void onLongClick(RecyclerView.ViewHolder vh) {
                if (vh.getLayoutPosition()!=list.size()-1) {
                    itemTouchHelper.startDrag(vh);
                    VibratorUtil.Vibrate(BBXActivity.this, 70);   //震动70ms
                 }
            }
            @Override
            public void onItemClick(RecyclerView.ViewHolder vh) {
                BaiBaoXiang item = list.get(vh.getLayoutPosition());
                Intent intent ;
                switch (item.getName()){
                    case "流量话费":
                        Toast.makeText(BBXActivity.this, "你点击的是 :"+item.getName(), Toast.LENGTH_SHORT).show();
                        break;
                    case "回首页":
                        intent = new Intent(BBXActivity.this,MainActivity.class);
                        startActivity(intent);
                        break;
                    case "家人守护":
                        intent = new Intent(MyApplication.getContext(), ProtectionFamilyActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        MyApplication.getContext().startActivity(intent);
                        break;
                }

//                Toast.makeText(BBXActivity.this,item.getId()+" "+item.getName(),Toast.LENGTH_SHORT).show();
            }
        });

        bbxRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public void initView() {


    }

    @Override
    public void onFinishDrag() {

    }

    public static class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> implements MyItemTouchCallback.ItemTouchAdapter{
        private Context mcontext;
        private List<BaiBaoXiang> mDataset;
        class  ViewHolder extends RecyclerView.ViewHolder{
            private TextView tv_item_name;
            private ImageView tv_item_icon;
            public ViewHolder(View itemView) {
                super(itemView);
//                WindowManager wm = (WindowManager) mcontext.getSystemService(Context.WINDOW_SERVICE);
//                int width = wm.getDefaultDisplay().getWidth();
//                ViewGroup.LayoutParams layoutParams = itemView.getLayoutParams();
//                layoutParams.height = width/4;
//                itemView.setLayoutParams(layoutParams);
                tv_item_name = (TextView) itemView.findViewById(R.id.tv_item_name);
                tv_item_icon = (ImageView) itemView.findViewById(R.id.tv_item_icon);
            }
        }
        public MyAdapter(List<BaiBaoXiang> myDataset) {
            this.mDataset = myDataset;
        }

        @Override
        public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_layout, parent, false);
            MyAdapter.ViewHolder viewHolder = new MyAdapter.ViewHolder(itemView);
            return viewHolder;

        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, int position) {
            final BaiBaoXiang bx=mDataset.get(position);
//            holder.mTextView.setText(bx.getName());

            holder.tv_item_name.setText(bx.getName());
            Drawable drawable = MyUtil.getBBXIcon(bx.getIcon());
            holder.tv_item_icon.setImageDrawable(drawable);
        }

        @Override
        public int getItemCount() {

            return mDataset.size();
        }

        //拖拽
        public void onMove(int fromPosition, int toPosition) {
            if (fromPosition==mDataset.size()-1 || toPosition==mDataset.size()-1){
                return;
            }
            if (fromPosition < toPosition) {
                for (int i = fromPosition; i < toPosition; i++) {
                    Collections.swap(mDataset, i, i + 1);
                }
            } else {
                for (int i = fromPosition; i > toPosition; i--) {
                    Collections.swap(mDataset, i, i - 1);
                }
            }
            notifyItemMoved(fromPosition, toPosition);
        }

        public void onSwiped(int position) {
            mDataset.remove(position);
            notifyItemRemoved(position);
        }


    }


}
